import pandas as pd
import requests
import json


# 登录界面
def login():
    url = "http://adm.ghmixloan.com/v2/login"
    data = {
        "account": "admin",
        "password": "121625Lin"
    }
    # json参数会将字典类型自动转为json数据
    response = requests.post(url, json=data)
    print(response.status_code)
    # 验证HTTP状态码是否为200
    if response.status_code == 200:
        # 提取响应结果中的token字段
        token = json.loads(response.text)["data"]["token"]
        print(token)
        return token
    else:
        print(f"接口请求失败，状态码：{response.status_code}")
        return None


# 订单管理界面
def use_token(token,mobile):
    # 请求头
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
        # 不同产品不同
        "Referer": "http://adm.ghmixloan.com/",
        # 不同产品不同
        "Host": "adm.ghmixloan.com",
        "lang": "ZH",
        "Accept": "application/json, text/plain, */*",
        # 关键token，记录登录唯一标识
        "Authorization": f"Bearer {token}"
    }
    # 请求参数
    params = {
        # 分配开始时间
        "mobile": f"{mobile}",
        "page": 1,
        "limit": 100,
    }
    url = "http://adm.ghmixloan.com/v2/biz/loan/review/query/need/00.do"
    # 响应体
    resp = requests.get(url, params=params, headers=headers)
    # print(resp.text)
    # print(json.loads(resp.text))
    # 响应状态
    print(resp.status_code)
    # json.loads()将json字符串转为python对象，json.dumps()将python对象转为json字符串
    count = json.loads(resp.text)["count"]
    print(count)
    return count


# 读取手机号的文档
df = pd.read_excel('手机号.xlsx', header=None, dtype={0: str})
# 第一列设置名手机号
df.columns = ['手机号']
# 第二列设置名申请次数，全为None
df['申请次数'] = None
# 登录返回token
token = login()
# 遍历手机号请求页面获得申请次数，再将申请次数返回到df中
for i in range(len(df)):
    mobile = df.loc[i,'手机号']
    count = use_token(token,mobile)
    df.loc[i, '申请次数'] = count
print(df)
